package com.healthcloud.service.hospital.impl;

import com.healthcloud.entity.HlHospitalEntity;
import com.healthcloud.entity.SysRoleEntity;
import com.healthcloud.service.HlHospitalService;
import com.healthcloud.utils.CommonMethod;
import com.healthcloud.utils.Constant;
import com.healthcloud.utils.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import com.healthcloud.dao.hospital.SysOrganiDao;
import com.healthcloud.entity.SysOrganiEntity;
import com.healthcloud.service.hospital.SysOrganiService;
import com.healthcloud.utils.PageParam;
import org.springframework.transaction.annotation.Transactional;



@Service("sysOrganiService")
@Transactional
public class SysOrganiServiceImpl implements SysOrganiService {
	@Autowired
	private SysOrganiDao sysOrganiDao;
	@Autowired
	private HlHospitalService hlHospitalService;
	
	@Override
	public SysOrganiEntity queryObject(Long id){
		return sysOrganiDao.queryObject(id);
	}
	
	@Override
	public List<SysOrganiEntity> queryList(SysOrganiEntity bean, PageParam pageParam){
		return sysOrganiDao.queryList(bean, pageParam);
	}
	
	@Override
	public int queryTotal(SysOrganiEntity bean){
		return sysOrganiDao.queryTotal(bean);
	}
	
	@Override
	public void save(SysOrganiEntity sysOrgani){
		if (sysOrgani.getParentId()==null) sysOrgani.setParentId(0l);
		sysOrgani.setCreateTime(new Date());
		sysOrgani.setUpdateTime(new Date());
		sysOrganiDao.save(sysOrgani);
	}
	
	@Override
	public void update(SysOrganiEntity sysOrgani){
		if (sysOrgani.getParentId()==null) sysOrgani.setParentId(0l);
		sysOrgani.setUpdateTime(new Date());
		sysOrganiDao.update(sysOrgani);
	}
	
	@Override
	public void delete(Integer id){
		sysOrganiDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		sysOrganiDao.deleteBatch(ids);
	}

	@Override
	public List<SysOrganiEntity> queryListAll() {
		return sysOrganiDao.queryListAll();
	}

	@Override
	public SysOrganiEntity queryByUserId(Long userId) {
		return sysOrganiDao.queryByUserId(userId);
	}


	/**
	 * 获取当前用户角色类型
	 * @return
	 */
	@Override
	public Integer getOrganiType() {
		Integer organiType = (Integer) CommonMethod.getSession().getAttribute("organiType");
		if (organiType!=null){//不属于任何机构
			return organiType;
		}
		Long userId = ShiroUtils.getUserId();
		organiType = -1;
		SysOrganiEntity organi = getOrgani();
		if (organi!=null){
			organiType = organi.getType();
		}else {//根据医院查询当前机构
			List<HlHospitalEntity> hospitals = hlHospitalService.getHospitalsByUserId(userId);
			if (hospitals!=null&&hospitals.size()>0) {//用户有医院
				HlHospitalEntity hospital = hospitals.get(0);
				if (hospital.getOrganiId()==null||hospital.getOrganiId()<=0){//没有机构,默认只有当前一家
					organiType = Constant.HOSPITAL_UNORG_TYPE;
				}
			}
		}
		CommonMethod.getSession().setAttribute("organiType", organiType);
		return organiType;
	}

	@Override
	public SysOrganiEntity getOrgani() {
		Long userId = ShiroUtils.getUserId();
		SysOrganiEntity organi = queryByUserId(userId);
		if (organi==null){
			List<HlHospitalEntity> hospitals = hlHospitalService.getHospitalsByUserId(userId);
			if (hospitals!=null&&hospitals.size()>0) {//用户有医院
				HlHospitalEntity hospital = hospitals.get(0);
				if (hospital.getOrganiId()!=null){
					organi = queryObject(hospital.getOrganiId());
				}
			}
		}
		return organi;
	}
}
